package com.leetcode.dataStruct.tree;

import com.leetcode.dataStruct.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class PathSum {
    public static void main(String[] args) {

    }

    List<List<Integer>> ret = new ArrayList<>();
    LinkedList<Integer> queue = new LinkedList<>();

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        dfs(root, targetSum);
        return ret;
    }

    private void dfs(TreeNode root, int targetSum) {
        if (root == null) {
            return;
        }
        queue.offerLast(root.val);
        targetSum -= root.val;
        if (root.left == null && root.right == null && targetSum == 0) {
            ret.add(new LinkedList<>(queue));
        }
        dfs(root.left, targetSum);
        dfs(root.right, targetSum);
        queue.pollLast();
    }
}
